<?php
##############################################
#
#   管理员列表
# @ Time : 2017-1-1 - 下午7:45:34
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Admin\Controller;
use Admin\Model\AdminUserModel;
use Admin\Model\UserGroupModel;
use Admin\Model\AdminBaseModel;

class AdminUserController extends AdminBaseController{
	
	/**
	 * 管理员列表
	 */
    public function userList(){
        $admin_user_model = new AdminUserModel();
        $user_data = $admin_user_model -> getAllUserData();
        $user_group = new UserGroupModel();
        $data = $user_group -> getUserGroup();
        $this -> assign('user_data', $user_data);
        $this -> assign('user_group', $data);
        $this -> display('user_list');
    }
	
	/**
	 * 添加管理员
	 */
	public function addUser(){
		$data=I('post.');
		$data['register_time'] = time();
		if(trim($data['password']) != trim($data['confirm_password'])){
			$this -> error('两次密码不一致');
		}else{
			$data['password'] = md5($data['password']);
			$admin_base_model = new AdminBaseModel('admin_users');
			$result = $admin_base_model -> addData($data);
			if($result){
				$group = array('uid' => $result, 'group_id' => $data['group_id']);
				$admin_base_model = new AdminBaseModel('auth_group_access');
				$admin_base_model -> addData($group);
			}
			$this -> success('添加成功', U('userList'), 2);
		}
	}

    /**
     *修改管理员
     * @Date 201703
     * @Author Linsenquan
     */
    public function adminEdit()
    {
        $id = I('get.id');
        if(IS_POST)
        {
            $data=I('post.');
            if($data['password'] && $data['confirm_password'])
            {
                if(trim($data['password']) != trim($data['confirm_password']))
                {
                    $this -> error('两次密码不一致','',2);
                }
                $data['password'] = md5($data['password']);
            }
            else
            {
                unset($data['password']);
            }
            //var_dump($data);
            //$result1 = M('AdminUsers')->where(array('id'=>$id))->save($data);//更新管理员信息
            $result1 = M('AdminUsers')->save($data);//更新管理员信息
            if($result1 !== false)
            {
                $group = array('uid' => $id, 'group_id' => $data['group_id']);
                M('auth_group_access')->where(array('uid'=>$id))->save($group);//更新管理员所属管理员组
            }
            else
            {
                $this -> error('修改失败，请重试！');
            }
            $this -> success('修改成功', U('userList'), 2);
            exit;
        }
        //取出管理员数据
        $user_group = new UserGroupModel();
        $data = $user_group -> getUserGroup();
        $this -> assign('user_group', $data);

        $result = M('AdminUsers')->where("id = $id")->find();
        $userGroup = M('auth_group_access')->where("uid=$id")->find();

        $this->assign('user',$result);
        $this->assign('userGroup',$userGroup);
        $this->display('AdminEdit');
    }

    /**
     *删除管理员
     * @Date 201707
     * @Author Lin
     */
    public function deleteAdmin()
    {
        if(!IS_AJAX) return;
        $admin_user_model = new AdminUserModel();
        $result =  $admin_user_model -> deleteAdmin(I('id'));
        if($result)
            $this->ajaxreturn(array('status'=>1,'message'=>'删除成功！'));
        else
            $this->ajaxReturn(array('status'=>0,'message'=>'删除失败,请重试！'));
    }
	
	
}